package org.jeecg.modules.electronicSeal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.electronicSeal.service.ElectronicSealService;
import org.jeecg.modules.electronicSeal.vo.RequestElectronic;
import org.jeecg.modules.electronicSeal.vo.ResponseStr;
import org.jeecg.modules.kuybCarTrain.entity.KuybCarTrain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;

@Api(tags = "kuyb_car_train")
@RestController
@RequestMapping("/electronic")
@Slf4j
public class ElectronicController {
    @Autowired
    ElectronicSealService sealService;

    /**
     * 提交个人信息
     * {"idcard":"132201199012210611","city":"邢台市","state":"河北省","firstname":"雪刚","lastname":"魏","mobile":"13051399951","policyId":"1456465341607178241"}
     *
     * @param electronic
     * @return
     */
    @AutoLog(value = "签章-提交个人信息")
    @ApiOperation(value = "签章-提交个人信息", notes = "签章-提交个人信息")
    @PostMapping(value = "/requestSubmitInfo")

    public Result<?> requestSubmitInfo(@RequestBody RequestElectronic electronic) {
        ResponseStr responseStr = sealService.requestSubmitInfo(electronic);
        return Result.OK(responseStr);
    }

    /**
     * 提交验证码
     * {"mobile":"13051399951","code":"123456","policyId":"1456465341607178241"}
     *
     * @param electronic
     * @return
     */
    @AutoLog(value = "签章-提交验证码")
    @ApiOperation(value = "签章-提交验证码", notes = "签章-提交验证码")
    @PostMapping(value = "/requestValidateInfo")
    public Result<?> requestValidateInfo(@RequestBody RequestElectronic electronic) {
        ResponseStr responseStr = sealService.requestValidateInfo(electronic);
        return Result.OK(responseStr);
    }

    String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        return base64;
    }

    @SneakyThrows
    String download2base64(String xiazdz) {
        int bytesum = 0;
        int byteread = 0;
        String base64 = "";
        URL url = new URL(xiazdz);

        try {
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();

            byte[] buffer = new byte[1204 * 1024];
            byte[] tmp = new byte[1204 * 1024 * 10];

            while ((byteread = is.read(buffer)) != -1) {
                System.arraycopy(buffer, 0, tmp, bytesum, byteread);
                bytesum += byteread;
            }

            byte[] result = new byte[bytesum];
            System.arraycopy(tmp, 0, result, 0, bytesum);
            base64 = Base64.getEncoder().encodeToString(result);

        } catch (Exception error) {
            error.printStackTrace();
        }

        return base64;
    }

    /**
     * 上传材料
     * {"mobile":"13051399951","pic1":"图片base64编码","pic2":"图片base64编码","pic3":"图片base64编码","policyId":"1456465341607178241"}
     *
     * @param electronic
     * @return
     */
    @AutoLog(value = "签章-上传材料")
    @ApiOperation(value = "签章-上传材料", notes = "签章-上传材料")
    @PostMapping(value = "/requestUploadPic")
    public Result<?> requestUploadPic(@RequestBody RequestElectronic electronic) {
        String base64 = download2base64(electronic.getPic1());
        electronic.setPic1(base64);

        base64 = download2base64(electronic.getPic2());
        electronic.setPic2(base64);

        base64 = download2base64(electronic.getPic3());
        electronic.setPic3(base64);

        ResponseStr responseStr = sealService.requestUploadPic(electronic);
        return Result.OK(responseStr);
    }

}
